package svc

import (
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/rest"
	"ns/app/applet/api/internal/config"
	"ns/app/applet/api/internal/middleware"
	"ns/app/applet/api/internal/model"
	"time"
)

type ServiceContext struct {
	Config    config.Config
	CheckAuth rest.Middleware

	UserModel           model.UserModel
	RoleModel           model.RoleModel
	UserEstateAuthModel model.UserEstateAuthModel
	ConfigModel         model.ConfigModel

	Redis *redis.Redis
}

func NewServiceContext(c config.Config) *ServiceContext {
	mysqlConn := sqlx.NewMysql(c.DataSource)
	return &ServiceContext{
		Config:              c,
		CheckAuth:           middleware.NewCheckAuthMiddleware().Handle,
		UserModel:           model.NewUserModel(mysqlConn, c.CacheRedis, cache.WithExpiry(7*24*time.Hour)),
		RoleModel:           model.NewRoleModel(mysqlConn, c.CacheRedis, cache.WithExpiry(7*24*time.Hour)),
		UserEstateAuthModel: model.NewUserEstateAuthModel(mysqlConn, c.CacheRedis, cache.WithExpiry(7*24*time.Hour)),
		ConfigModel:         model.NewConfigModel(mysqlConn, c.CacheRedis, cache.WithExpiry(7*24*time.Hour)),
		Redis:               redis.MustNewRedis(c.CacheRedis[0].RedisConf),
	}
}
